import { mockRequestClient as requestClient } from '#/api/request';

// 业务场景相关接口
export interface BusinessContext {
  id: string;
  name: string;
  description: string;
  icon: string;
  status: number;
  createTime: string;
  updateTime: string;
}

export interface ServiceItem {
  id: string;
  name: string;
  description: string;
  price: number;
  duration: number;
  contextId: string;
  status: number;
  createTime: string;
  updateTime: string;
}

export interface Booking {
  id: string;
  bookingNo: string;
  serviceId: string;
  serviceName: string;
  contextId: string;
  contextName: string;
  customerName: string;
  phoneNumber: string;
  wechatId?: string;
  memberCard?: string;
  bookingDate: string;
  timeSlot: string;
  status: string;
  queueTicket?: string;
  queuePosition?: number;
  estimatedWaitTime?: string;
  remarks?: string;
  createTime: string;
  updateTime: string;
}

export interface QueueTicket {
  id: string;
  ticketNumber: string;
  bookingId: string;
  serviceId: string;
  customerName: string;
  phoneNumber: string;
  position: number;
  status: string;
  estimatedWaitTime: string;
  serviceWindow?: string;
  createTime: string;
  updateTime: string;
}

export interface TimeSlot {
  id: string;
  time: string;
  available: boolean;
  remainingCapacity: number;
}

// 获取业务场景列表
export function getBusinessContexts() {
  return requestClient.get<BusinessContext[]>('/booking/business-contexts');
}

// 获取服务项目列表
export function getServiceItems(params: {
  contextId?: string;
  status?: number;
}) {
  return requestClient.get<ServiceItem[]>('/booking/service-items', { params });
}

// 获取可用时间段
export function getAvailableTimeSlots(params: {
  date: string;
  serviceId: string;
}) {
  return requestClient.get<TimeSlot[]>('/booking/time-slots', { params });
}

// 创建预约
export function createBooking(data: {
  bookingDate: string;
  contextId: string;
  customerName: string;
  memberCard?: string;
  phoneNumber: string;
  remarks?: string;
  serviceId: string;
  timeSlot: string;
  wechatId?: string;
}) {
  return requestClient.post<Booking>('/booking/bookings', data);
}

// 获取我的预约列表
export function getMyBookings(params: {
  page?: number;
  pageSize?: number;
  status?: string;
}) {
  return requestClient.get<{ list: Booking[]; total: number }>(
    '/booking/my-bookings',
    { params },
  );
}

// 取消预约
export function cancelBooking(bookingId: string) {
  return requestClient.put(`/booking/bookings/${bookingId}/cancel`);
}

// 取号排队
export function takeQueueTicket(bookingId: string) {
  return requestClient.post<QueueTicket>(
    `/booking/bookings/${bookingId}/queue-ticket`,
  );
}

// 获取排队状态
export function getQueueStatus() {
  return requestClient.get<{
    windows: Array<{
      currentCustomer?: {
        customerName: string;
        startTime: string;
        ticketNumber: string;
      };
      id: string;
      name: string;
      status: string;
    }>;
  }>('/booking/queue/status');
}

// 获取我的排队信息
export function getMyQueueInfo() {
  return requestClient.get<{
    estimatedWaitTime: string;
    position: number;
    serviceWindow: string;
    ticketNumber: string;
    totalInQueue: number;
  }>('/booking/queue/my-info');
}

// 获取排队列表
export function getQueueList() {
  return requestClient.get<
    Array<{
      customerName: string;
      status: string;
      ticketNumber: string;
    }>
  >('/booking/queue/list');
}

// 管理端接口
export interface BookingQueryParams {
  page?: number;
  pageSize?: number;
  contextId?: string;
  serviceId?: string;
  status?: string;
  customerName?: string;
  phoneNumber?: string;
  startDate?: string;
  endDate?: string;
}

// 管理端获取预约列表
export function getBookingList(params: BookingQueryParams) {
  return requestClient.get<{ list: Booking[]; total: number }>(
    '/booking/admin/bookings',
    { params },
  );
}

// 管理端确认预约
export function confirmBooking(bookingId: string) {
  return requestClient.put(`/booking/admin/bookings/${bookingId}/confirm`);
}

// 管理端开始服务
export function startService(bookingId: string) {
  return requestClient.put(`/booking/admin/bookings/${bookingId}/start`);
}

// 管理端完成服务
export function completeService(bookingId: string) {
  return requestClient.put(`/booking/admin/bookings/${bookingId}/complete`);
}

// 管理端标记爽约
export function markNoShow(bookingId: string) {
  return requestClient.put(`/booking/admin/bookings/${bookingId}/no-show`);
}

// 管理端叫号
export function callNextTicket(windowId: string) {
  return requestClient.post(`/booking/admin/windows/${windowId}/call-next`);
}

// 管理端获取服务窗口列表
export function getServiceWindows() {
  return requestClient.get<
    Array<{
      currentCustomer?: {
        customerName: string;
        startTime: string;
        ticketNumber: string;
      };
      id: string;
      name: string;
      status: string;
    }>
  >('/booking/admin/windows');
}

// 管理端更新服务窗口状态
export function updateWindowStatus(windowId: string, status: string) {
  return requestClient.put(`/booking/admin/windows/${windowId}/status`, {
    status,
  });
}

// 报表统计接口
export interface BookingStats {
  totalBookings: number;
  confirmedBookings: number;
  completedBookings: number;
  cancelledBookings: number;
  averageWaitTime: number;
  noShowRate: number;
}

// 获取预约统计
export function getBookingStats(params: {
  contextId?: string;
  endDate?: string;
  startDate?: string;
}) {
  return requestClient.get<BookingStats>('/booking/admin/stats', { params });
}

// 获取预约趋势数据
export function getBookingTrends(params: {
  contextId?: string;
  endDate: string;
  groupBy: 'day' | 'hour';
  startDate: string;
}) {
  return requestClient.get<
    Array<{
      bookings: number;
      cancelled: number;
      completed: number;
      date: string;
    }>
  >('/booking/admin/trends', { params });
}

// 通知相关接口
export interface Notification {
  id: string;
  type: 'app' | 'sms' | 'wechat';
  title: string;
  content: string;
  recipient: string;
  status: string;
  sendTime?: string;
  createTime: string;
}

// 发送通知
export function sendNotification(data: {
  bookingId?: string;
  content: string;
  recipient: string;
  title: string;
  type: 'app' | 'sms' | 'wechat';
}) {
  return requestClient.post<Notification>('/booking/notifications', data);
}

// 获取通知历史
export function getNotificationHistory(params: {
  endDate?: string;
  page?: number;
  pageSize?: number;
  startDate?: string;
  status?: string;
  type?: string;
}) {
  return requestClient.get<{ list: Notification[]; total: number }>(
    '/booking/admin/notifications',
    { params },
  );
}
